<?php

namespace app\models;

use app\models\role\AdminRole;
use app\models\role\Permission;
use app\models\role\RoleMenu;
use Yii;

/**
 * This is the model class for table "admin".
 *
 * @property int $id
 * @property string $username
 * @property string $password
 * @property string $role 角色
 * @property string $create_time
 * @property string $update_time
 * @property string $avatar
 * @property string $nickname
 */
class Admin extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'admin';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['create_time', 'update_time'], 'safe'],
            [['username'], 'string', 'max' => 50],
            [['password'], 'string', 'max' => 32],
            [['role', 'avatar', 'nickname'], 'string', 'max' => 255],
            [['username'], 'unique'],

            [['can_delete'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'role' => 'Role',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'avatar' => 'Avatar',
            'nickname' => 'Nickname',
        ];
    }

    /**
     * Save timestamp
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    public static function login($username, $password)
    {
        return self::find()
            ->where(['username' => $username, 'password' => $password])
            ->one();
    }

    public static function getInfo($username)
    {
        $user = self::find()
            ->where(['username' => $username])
            ->asArray()
            ->one();

        if ($user) {
            unset($user['password']);
            // 需要定义角色(前端需要)
            // $user['roles'] = [$user['role']];


            $user['roles'] = self::getMenuListByRole($user['id']);
            $user['name'] = $user['username'];

            return $user;
        }

        return false;
    }

    // 直接把菜单列表给前端
    public static function getMenuListByRole($user_id)
    {
        // 用户有哪些角色
        $role_ids = AdminRole::find()
            ->where(['admin_id' => $user_id])
            ->select(['role_id'])
            ->column();

        // var_dump($role_ids);exit;

        // 角色有哪些菜单
        $menu_ids = Permission::find()
            ->where(['role_id' => $role_ids])
            ->select(['menu_id'])
            ->column();
        // var_dump($menu_ids);exit;

        // 菜单有哪些名称(这里拿链接,去掉首/, 对应前端)
        $urls = RoleMenu::find()
            ->where(['id' => $menu_ids])
            ->select(['url'])
            ->column();

        // 去掉 /
        foreach ($urls as &$url) {
            $url = substr($url, 1);
        }

        return $urls;

    }
}
